package com.yang.mybatis.proxy;

import com.yang.mybatis.session.IMybatisSqlSession;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class MapperProxy<T> implements InvocationHandler {

    private Class<T> mapperInterface;

    private IMybatisSqlSession sqlSession;

    public MapperProxy(Class<T> mapperInterface, IMybatisSqlSession mybatisSqlSession) {
        this.mapperInterface = mapperInterface;
        this.sqlSession = mybatisSqlSession;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (Object.class.equals(method.getDeclaringClass())) {
            return method.invoke(this, args);
        }
        String methodName = this.mapperInterface.getName() + "." + method.getName();
        return sqlSession.execute(methodName, args);
    }
}
